from pymongo import MongoClient
from classCluster import Cluster
from classEdge import Edge
import json
import pickle

#server_address = "127.0.0.1:27017"
server_address = "127.0.0.1:27039"
def storeData(target):
    f = open("data/result/" + target + ".json")
    file_content = pickle.load(f)
    f.close()
    client = MongoClient("mongodb://" + server_address)
    db = client['local']
    storeInfluenceGraph(file_content, db, target)
    storeCluster(file_content, db, target.split("_")[0], int(target.split("_")[1]))
    storeNode(file_content, db, target.split("_")[0], int(target.split("_")[1]))
    return db

def storeNode(content, database, rootid, cluster):
    for c in list(content['cluster']):
        cluster = content['cluster'][c]
        nodelist = cluster.nodes
        for node in nodelist:
            node['rootid'] = str(rootid)
            node["cluster_count"] = str(cluster)
            database.node.insert_one(node)

def storeCluster(content, database, rootid, cluster):
    for c in list(content['cluster']):
        cluster = content['cluster'][c]
        result = dict()
        result["rootid"] = str(rootid)
        result["cluster_count"] = str(cluster)
        result["cluster_name"] = str(c)
        result["nodeTitles"] = list()
        nodelist = cluster.nodes
        for node in nodelist:
            tmp = list()
            tmp.append(str(node['id']))
            tmp.append(str(node['title']))
            tmp.append(str(node['citation_count']))
            tmp.append(int(node['year']))
            result["nodeTitles"].append(tmp)
        result["nodeTitles"].sort(key = keyForNodesSort)
        database.cluster.insert_one(result)

def keyForNodesSort(a):
    return int(a[2])

def storeInfluenceGraph(content, database, target):
    result = dict()
    result["target"] = str(target)
    # for backward compatibility
    result["type"] = "single"
    # construct "cluster" part
    result["cluster"] = dict()
    cluster_ids = content["cluster"].keys()
    for i in cluster_ids:
        cluster = content["cluster"][i]
        result["cluster"][i] = dict()
        result["cluster"][i]["focused"] = "false"
        result["cluster"][i]["keywords"] = cluster.keywords
        result["cluster"][i]["nodeidlist"] = cluster.nodeidlist
        if i == "300":
            result["cluster"][i]["focused"] = "true"
            result["cluster"][i]["author"] = cluster.nodes[0]['firstauthor']
            result["cluster"][i]["venue"] = cluster.nodes[0]['venue']
            result["cluster"][i]["title"] = cluster.nodes[0]['title']
            result["cluster"][i]["summary"] = cluster.nodes[0]['summary']
    # construct "edge" part
    result["edge"] = list()
    for edge in content["edge"]:
        curr = dict()
        curr["source"] = edge.source
        curr["target"] = edge.target
        curr["weight"] = edge.weight
        curr["flow"] = edge.flow
        curr["edge_detail"] = edge.edge_detail
        # for backward compatibility
        curr["agg_count"] = edge.agg_count
        curr["PROP_WEIGHT"] = edge.PROP_WEIGHT
        curr["assists"] = edge.assists
        result["edge"].append(curr)
    print(result)
    database.graph.insert_one(result)
